//
//  PersonInfoPhotoCell.m
//  carcareIOS
//
//  Created by wr on 15/11/13.
//  Copyright © 2015年 baozun. All rights reserved.
//

#import "PersonInfoPhotoCell.h"
#import "AIUserInfo.h"

//#define CELL_HEAD_HEIGHT (64)

#define CELL_HEIGHT ( (__MAIN_WIDTH == 320 ? 190 : 204) * __MAIN_RATIO_375)

#define CELL_FOOT_HEIGHT (57 * __MAIN_RATIO_375)

typedef NS_ENUM(NSInteger , CELL_LABLE_TYPE) {
    CELL_LABLE_TAG = 20
};

@interface PersonInfoPhotoCell()

@property (nonatomic , strong)UIImageView * iconBGImageV;

@property (nonatomic , strong)UIImageView * iconImageV;

@property (nonatomic , readonly) NSArray * footLabelArr;

@property (nonatomic , strong) UIImageView * myOrderCornerIV;

@end
@implementation PersonInfoPhotoCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        
//        ImageNamed(@"userUseIconBackImage")
        self.iconBGImageV = [Factory imageViewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, CELL_HEIGHT) image:nil onView:self];
        self.iconBGImageV.backgroundColor = [Theme mainNavColor];
//        UIImageView * outIconBGImageV = [Factory imageViewWithFrame:self.iconBGImageV.bounds image:nil onView:self];
//        outIconBGImageV.backgroundColor = COLOR_RGBA(0, 0, 0, 0.6);
        
        CGFloat iconImageW = 85 * __MAIN_RATIO_375;
        CGFloat iconImageX = (__MAIN_WIDTH - iconImageW) / 2;
        CGFloat iconImageY = 7 * __MAIN_RATIO_375 ;
        self.iconImageV = [Factory imageViewWithFrame:CGRectMake(iconImageX, iconImageY, iconImageW, iconImageW) image:ImageNamed(@"defaultIcon") onView:self];
        [self.iconImageV setImageWithURL:[NSURL URLWithString:USERINFO.showImageURL]];
        self.iconImageV.layer.cornerRadius = iconImageW / 2;
        self.iconImageV.layer.masksToBounds = YES;
        self.iconImageV.layer.borderColor = [UIColor whiteColor].CGColor;
        self.iconImageV.layer.borderWidth = 1.5;
        
        //换头像按钮
        UIButton * iconImageBtn = [Factory buttonWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, CELL_HEIGHT - CELL_FOOT_HEIGHT) withImageName:@"" click:nil onView:self];
        iconImageBtn.backgroundColor = [UIColor clearColor];
        iconImageBtn.tag = BTN_TYPE_ICON;
        [iconImageBtn addTarget:self action:@selector(photoBtnBlock:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat nameLabelH = 20 ;
        CGFloat nameLabelY = self.iconImageV.bottom + (CELL_HEIGHT - self.iconImageV.bottom - CELL_FOOT_HEIGHT - nameLabelH) / 2;
        CGFloat nameLabelX = 22 * __MAIN_RATIO_375;
        CGFloat nameLabelW = __MAIN_WIDTH - nameLabelX * 2;
//        CGFloat carNameLabelY = nameLabelY + nameLabelH + 4;//11
        [Factory labelWithFrame:CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH) font:FONT_DEFAULT_Light(20 * __MAIN_RATIO_375) text:([USERINFO.showName isNotNil]?USERINFO.showName:@"用户名") textColor:[UIColor whiteColor] onView:self textAlignment:NSTextAlignmentCenter].backgroundColor = [UIColor clearColor];
        //[Factory labelWithFrame:CGRectMake(nameLabelX, carNameLabelY, nameLabelW, nameLabelH) font:FONT_DEFAULT_Light(12) text:([USERINFO.carBrand isNotNil]?USERINFO.carBrand:@"车型") textColor:COLOR_RGBA(0xFF, 0xFF, 0xFF, 0.6) onView:self textAlignment:NSTextAlignmentLeft];
        
        [self reloadUserPhoto];
    
        UIView * smallBackView = [[[UIView viewWithFrame:CGRectMake(0, CELL_HEIGHT - CELL_FOOT_HEIGHT, __MAIN_WIDTH, CELL_FOOT_HEIGHT)] resetBackgroundColor:COLOR_RGBA(0, 0, 0, 0.15)] setupOnView:self];
        
        CGFloat footLabelW = __MAIN_WIDTH;

        CGFloat footLabelH = 18;
        CGFloat footLabelAndLabelY = 1 * __MAIN_RATIO_375;
        CGFloat footLabelY = (smallBackView.height - footLabelAndLabelY - footLabelH * 2) / 2;
//        [[[UIView viewWithFrame:CGRectMake(__MAIN_WIDTH / 2, 10 * __MAIN_RATIO_375, 0.5, smallBackView.height - 10 * __MAIN_RATIO_375 * 2)] resetBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.6]] setupOnView:smallBackView];
        for (int index = 0; index < self.footLabelArr.count; index ++) {
            if (index == 0) {
                continue;
            }
            CGFloat footLabelX = 0;
            UILabel * numLabel = [Factory labelWithFrame:CGRectMake(footLabelX, footLabelY, footLabelW, footLabelH) font:FONT_DEFAULT_Light(18 * __MAIN_RATIO_375) text:@"" textColor:[UIColor whiteColor] onView:smallBackView textAlignment:NSTextAlignmentCenter];
            numLabel.tag = CELL_LABLE_TAG + index;
            //添加我的订单icon
            if(index == 0)self.myOrderCornerIV = [Factory imageViewWithCenter:CGPointMake(numLabel.left + numLabel.width / 2, numLabel.top + numLabel.height / 2) image:ImageNamed(@"myOrder_Icon_Image") onView:smallBackView];
            [Factory labelWithFrame:CGRectMake(footLabelX, numLabel.bottom + footLabelAndLabelY, footLabelW, footLabelH) font:FONT_DEFAULT_Light(12 * __MAIN_RATIO_375) text:self.footLabelArr[index] textColor:COLOR_RGBA(0xFF, 0xFF, 0xFF, 0.6) onView:smallBackView textAlignment:NSTextAlignmentCenter];
            
            UIButton * button = [[[UIButton buttonWithFrame:CGRectMake(footLabelW * index, 0, footLabelW, smallBackView.height) title:nil textColor:[UIColor clearColor] font:FONT_BUTTON click:nil] resetBackgroundColor:[UIColor clearColor]] setupOnView:smallBackView];
            
            button.tag = (index == 0 ? BTN_TYPE_CAR_COUNPON : BTN_TYPE_SURPLUS_MONEY);
            [button addTarget:self action:@selector(photoBtnBlock:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [[[UIView viewWithFrame:CGRectMake(0, CELL_HEIGHT - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:self];
    }
    return self;
}
- (void)photoBtnBlock:(UIButton *)sender
{
    if (self.photoBtnBlock) self.photoBtnBlock(sender.tag);
}
- (NSArray *)footLabelArr
{
    return @[@"我的订单",@"剩余挣点"];
}
- (void)setIsShowRedCorner:(BOOL)isShowRedCorner
{
    if (isShowRedCorner) {
        [self.myOrderCornerIV showRemind:YES center:CGPointMake(self.myOrderCornerIV.width + 3, 2)];
    }else
    {
        [self.myOrderCornerIV showRemind:NO center:CGPointMake(0, 0)];
    }
}
- (void)reloadUserPhoto
{
    //[self.iconBGImageV setImageWithURL:[NSURL URLWithString:USERINFO.showImageURL]];
    
    __weak __typeof(self) wself = self;
    [self.iconImageV setImageWithURLRequest:[ImageHelper requestURLWithURLStr:USERINFO.showImageURL] placeholderImage:ImageNamed(@"defaultIcon.png") success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        wself.iconImageV.image = image;
    } failure:nil];
}
+ (CGFloat)getCellHeight
{
    return CELL_HEIGHT;
}

-(void)setSurplusMoneyStr:(NSString *)surplusMoneyStr
{
    UILabel * surplusMoneyLabel = (UILabel *)[self viewWithTag:CELL_LABLE_TAG + 1];
    surplusMoneyLabel.text = surplusMoneyStr;
}
//- (void)setGradeStr:(NSString *)gradeStr
//{
//    UILabel * gradeLable = (UILabel *)[self viewWithTag:CELL_LABLE_TAG + 0];
//    gradeLable.text = [NSString stringWithFormat:@"Lv%@",gradeStr];
//}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
